package com.example.concurrentprogramming8016.thread;

/**
 * @author liyanhao
 * @Date 2023-01-03 16:36
 */
public class WaitDemo {
    private static Object lock = new Object();
    private static boolean flag = true;

    public static void main(String[] args) {

        Thread thread = new Thread(() -> {
            synchronized (lock) {
                while (flag) {
                    try {
                        try {
                            System.out.println("wait start--");
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("wait end......");
            }
        });
        thread.start();

        Thread thread1 = new Thread(() -> {
            if (flag) {
                synchronized (lock) {
                    if (flag) {
                        lock.notify();
                        System.out.println("notify.....");
                        flag = false;
                    }
                }
            }
        });
        thread1.start();

    }
}
